package com.wordsay.parser.impl;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.wordsay.parser.ChatParser;

public class FxChatParserProxy implements ChatParser {

	private static Logger log = LogManager.getLogger(FxChatParserProxy.class.getName());
	//fileName pattern: 
	
	@Override
	public Object parse(String chatFilePath) throws Exception {	
		
		File chatFile = new File(chatFilePath);
		if(!chatFile.exists()){
			log.error("chat file not exist! "+chatFilePath);
			throw new Exception("chat file not exist! "+chatFilePath);
		}
		
		String fileName = chatFile.getName();
		if(!fileName.contains("(")){
			return null;
		}
		
		ChatParser chatParser = null;
		if(this.isGroupChat(fileName)){
			
			chatParser = new FxGroupChatParser();
			
			
		}else{
			
			chatParser = new FxPersonChatParser();
			
		}	
		
		return chatParser.parse(chatFilePath);
	}
	
	/**
	 * distinguish whether is the group chat depend on the chat file name
	 * @param fileName
	 * @return
	 */
	private boolean isGroupChat(String fileName){
		
		int x = fileName.indexOf("(");
		String sign = fileName.substring(x+1,fileName.indexOf(")"));
		System.out.println(fileName+"--->"+x+"   "+sign);

		if(sign.contains("PG") || sign.contains("DG")){
			return true;
		}
		
		return false;	
	}
	
	
}
